package com.doit.day04

import scala.io.Source

/**
 * left join  还是join
 */
object _03_join案例 {
  def main(args: Array[String]): Unit = {
    //1.读取两个文件
    val order: List[String] = Source.fromFile("data/order.txt").getLines().toList
    val user: List[String] = Source.fromFile("data/user.txt").getLines().toList

    //2.拼接起来  两个文件拼接  map映射 把两个串起来
    val orderArray: List[Array[String]] = order.map(_.split(","))
    val userArray: List[Array[String]] = user.map(_.split(","))

    val orders: List[(String, String, String)] = orderArray.map(tp => {
      val order_id: String = tp(0)
      val user_id: String = tp(1)
      val price: String = tp(2)
      (order_id, user_id, price)
    })


    val users: Map[String, (String, String, String)] = userArray.map(tp => {
      val user_id: String = tp(0)
      val name: String = tp(1)
      val age: String = tp(2)
      val nickName: String = tp(3)
      (user_id,(name, age,nickName))
    }).toMap

    // 做join拼接  left join
    orders.map(tp=>{
      val user_id: String = tp._2
      val tuple3: Option[(String, String, String)] = users.get(user_id)
      if (tuple3.isDefined) {
        val tp2: (String, String, String) = tuple3.get
        (tp._1, tp._3, tp2._2, tp2._3, tp2._1)
      }
    }).foreach(println)






  }
}
